package top.csjss.hospital.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import top.csjss.hospital.enums.HospitalExceptionEnums;
import top.csjss.hospital.exception.HospitalException;
import top.csjss.hospital.service.SysUserService;
import top.csjss.hospital.vo.R;

/**
 * @Author ztf
 * @Date 15:49 2021/10/15
 **/
@RestController
public class LoginController {
    private Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录功能
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/sel/doLogin")
    public R doLogin(String loginname,String pwd){
        //请求校验参数
        if (StringUtils.isEmpty(loginname) || StringUtils.isEmpty(pwd)){
            log.info("【登录功能】请求参数异常！用户名或密码为空");
            throw new HospitalException(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //获取Subject
        Subject subject=SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(loginname,pwd));
        return R.ok("登陆成功");
    }

    @GetMapping("/sel/logout")
    public R logout(){
        //获取主体
        Subject subject= SecurityUtils.getSubject();
        //退出登录
        subject.logout();

        return R.ok("退出登录成功！");
    }
}
